package com.lbc.state;

//  观察测试线程的状态
//  new 就绪      运行           阻塞     dead
//  NEW RUNNABLE  TIMED_WAITING  BLOCKED  TERMINATED
//  等待  WAITING
public class TestState {

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(()->{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //  state = TIMED_WAITING  //  等待，在睡觉
                System.out.println("/////////");
                //  state = TERMINATED  //  结束
            });

            //  观察状态
            Thread.State state = thread.getState();
            System.out.println("state = " + state);  //  NEW

            //  观察启动后
            thread.start();  //  启动线程
            state = thread.getState();
            System.out.println("state = " + state);  //  RUNNABLE

            while (state != Thread.State.TERMINATED) {  //  只要线程不终止，就一直运行
                Thread.sleep(100);
                state = thread.getState();  //  更新线程状态
                System.out.println("state = " + state);  //  输出状态
            }
        }
    }
}
